﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.ManagePage
{
    public partial class ManageSubMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdMenu.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuChild();
            grdMenu.DataBind();
        }

        protected void grdMenu_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    BackEndMenu backEndMenu = new BackEndMenu();
                    var txtNameNew = (TextBox)e.Item.FindControl("txtName");
                    var txtOrderDisplayNew = (TextBox)e.Item.FindControl("txtOrderDisplay");
                    DropDownList cboParent = (DropDownList) e.Item.FindControl("cboParent");
                    backEndMenu.ParentId = Convert.ToInt32(cboParent.SelectedValue);
                    backEndMenu.Name = txtNameNew.Text;
                    try
                    {
                        backEndMenu.OrderDisplay = Convert.ToInt32(txtOrderDisplayNew.Text);
                    }
                    catch (Exception)
                    {
                        backEndMenu.OrderDisplay = 10;
                    }
                    backEndMenu.IsActive = true;
                    A.Controller.BackEndMenuController.Insert(backEndMenu);
                    break;
                case "cmdUpdate":
                    BackEndMenu backEndMenuUpdate = A.Controller.BackEndMenuController.GetBackEndMenuById(e.CommandArgument);
                    var txtName = (TextBox)e.Item.FindControl("txtName");
                    var txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                    var chkActive = (CheckBox)e.Item.FindControl("chkActive");
                    DropDownList cboParent1 = (DropDownList) e.Item.FindControl("cboParent");
                    backEndMenuUpdate.ParentId = Convert.ToInt32(cboParent1.SelectedValue);
                    backEndMenuUpdate.Name = txtName.Text;
                    try
                    {
                        backEndMenuUpdate.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                    }
                    catch (Exception)
                    {
                    }
                    backEndMenuUpdate.IsActive = chkActive.Checked;
                    A.Controller.BackEndMenuController.Update();
                    break;
                case "cmdDelete":
                    BackEndMenu backEndMenuDelete = A.Controller.BackEndMenuController.GetBackEndMenuById(e.CommandArgument);
                    A.Controller.BackEndMenuController.Delete(backEndMenuDelete);
                    break;
            }
            BindData();
        }

        protected void grdMenu_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList cboParent = (DropDownList) e.Item.FindControl("cboParent");
                cboParent.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuParent();
                cboParent.DataTextField = "Name";
                cboParent.DataValueField = "Id";
                cboParent.DataBind();
                cboParent.SelectedValue = DataBinder.Eval(e.Item.DataItem, "ParentId").ToString();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList cboParent = (DropDownList) e.Item.FindControl("cboParent");
                cboParent.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuParent();
                cboParent.DataTextField = "Name";
                cboParent.DataValueField = "Id";
                cboParent.DataBind();
            }
        }
    }
}